﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmMttoSocio : Form
    {
        [Dependency]
        public ISocioBL socioBL { get; set; }

        [Dependency]
        public ITipoSocioBL tipSocioBL { get; set; }

        public FrmMttoSocio()
        {
            InitializeComponent();
            this.BuildUp();
        }

        bool isNew;

        public void NuevaSocio()
        {
            isNew = true;
            sOCIOBindingSource.AddNew();
        }

        public SOCIO _socio { get; set; }

        public void EditarSocio()
        {
            isNew = false;
            sOCIOBindingSource.DataSource = _socio;
        }

        private void FrmMttoSocio_Load(object sender, EventArgs e)
        {

        }

        private void btnTipoSocio_Click(object sender, EventArgs e)
        {
            FrmTipoSocio frm = new FrmTipoSocio();
            frm.Extend();
            frm.ShowDialog();

            if (frm.currentTipoSocio != null)
            {
                var nuevoTipSocio = tipSocioBL.GetFromTipoSocioById(frm.currentTipoSocio.IdTipoSocio);
                var socio = sOCIOBindingSource.Current as SOCIO;
                socio.TIPO_SOCIO = nuevoTipSocio;
                socio.IdTipoSocio = nuevoTipSocio.IdTipoSocio;
                sOCIOBindingSource.ResetBindings(true);
            }
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var socio = sOCIOBindingSource.Current as SOCIO;

            var msg = "ERROR";

            if (socio != null)
            {
                if (isNew == true)
                {
                    try
                    {
                        socio.RazonSocial = razonSocialTextBox.Text.ToUpper();
                        socioBL.AddSocio(socio);
                        MessageBox.Show("Registro Correcto", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (isNew == false)
                {
                    try
                    {
                        socio.RazonSocial = razonSocialTextBox.Text.ToUpper();
                        socioBL.UpdateSocio(socio);
                        MessageBox.Show("Modificación Correcta", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void rucTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void razonSocialTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var amberson = Convert.ToString("&");
            var punto = Convert.ToString(".");
            var guion = Convert.ToString("-");

            string cadena = amberson + punto + guion + (char)8;

            if (Char.IsLetter(e.KeyChar) || Char.IsNumber(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

      
    }
}
